package com.teen9g.infoplatformserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teen9g.entity.Comment;
import com.teen9g.entity.CommentLike;
import com.teen9g.vo.NoReplyCommentVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {

    @Delete("delete from comment where root_comment_id = #{commentId}")
    int deleteRelatedSubComment(Integer commentId);


    List<NoReplyCommentVO> selectNoReplyComment(@Param("relatedCategory") String relatedCategory,
                                                @Param("relatedId") Integer relatedId);

    List<NoReplyCommentVO> selectReplyComment(@Param("relatedCategory") String relatedCategory,
                                              @Param("relatedId") Integer relatedId,
                                              @Param("rootCommentId") Integer commentId);

    @Select("select count(1) from comment_like where user_id = #{userId} and comment_id = #{commentId}")
    Integer selectLikeTrack(@Param("userId") Integer userId, @Param("commentId") Integer commentId);

    @Delete("delete from comment_like where user_id = #{userId} and comment_id = #{commentId}")
    void deleteLikeTrack(@Param("userId") Integer userId, @Param("commentId") Integer commentId);

    @Insert("insert into comment_like (user_id, comment_id, created_at) values (#{userId}, #{commentId}, #{createdAt})")
    void insertLikeTrack(CommentLike commentLike);

    @Select("select count(1) from comment where related_id = #{relatedId} " +
            "and related_category = #{relatedCategory} and root_comment_id is null")
    Integer selectTotal(@Param("relatedId") Integer relatedId,
                        @Param("relatedCategory") String relatedCategory);

    @Update("update comment set like_count = like_count + #{updateCount} " +
            "where comment_id = #{commentId}")
    void updateLikeCount(@Param("commentId") Integer commentId,
                         @Param("updateCount") int updateCount);

    @Update("update comment set reply_count = reply_count + #{updateCount} " +
            "where comment_id = #{rootCommentId}")
    void updateReplyCount(@Param("rootCommentId") Integer rootCommentId,
                          @Param("updateCount") int updateCount);
}
